package copy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import copy.com.BaseContext;
import copy.com.R;
import copy.pojo.Orders;
import copy.pojo.ShoppingCart;
import copy.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author 小思
 * @Description //TODO
 * @Date $ $
 * @Param $
 * @return $
 **/

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * 使用线程中的user_id
     * 参数地址id
     * 先查购物车数据
     * 查用户数据
     * 查地址数据 参数中取出地址id
     * 查不出来抛异常即可
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("");
    }

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> ordersPage = new Page<>();
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(number != null, Orders::getNumber, number)
                .between(beginTime != null && endTime != null,Orders::getOrderTime, beginTime, endTime);
//                .eq(orders.getOrderTime() != null, Orders::getOrderTime, orders.getOrderTime());
        ordersService.page(ordersPage, qw);
        return R.success(ordersPage);

    }
}












